/******************************************************************************
* ChatBubble.cpp - 
* 
* Copyright 2011-2016 Guangzhou Timetop Electronics Technology Co.,Ltd.
* 
* DESCRIPTION: - 
*     -
* modification history
* 
* 01a, 31oct2012, Mr. Huanbiao Xie  written
*
******************************************************************************/
#include "ChatBubble.h"

//==============================================
LPOINT operator+(const LPOINT&pt1,const LPOINT&pt2)
{
	LPOINT tmp;
	tmp.lx=pt1.lx+pt2.lx;
	tmp.ly=pt1.ly+pt2.ly;
	return tmp;
}
LPOINT operator-(const LPOINT&pt1,const LPOINT&pt2)
{
	LPOINT tmp;
	tmp.lx=pt1.lx-pt2.lx;
	tmp.ly=pt1.ly-pt2.ly;
	return tmp;
}
//====================================================
CChatBubble::CChatBubble()
{
	m_iFaceFrame=0;
	m_dwShowTime=0;
	m_Scene=eSceneReady;
	for(int i=0;i<C_PLAYER_COUNT;i++)
	{
		//m_ptLocation[i]=g_ptBubble[i];
		m_dwStartTime[i]=0;
		m_hBubble[i][0]=INVALID_HSPRITE_VALUE;
		m_hBubble[i][1]=INVALID_HSPRITE_VALUE;
		m_bIsShow[i]=eBubbleTypeNull;
		m_ChatType[i]=-1;
		m_iSex[i]=-1;
	}
	Init(C_SHOWCHATTIME,C_FACEFRAME);
}
CChatBubble::~CChatBubble()
{
}
void CChatBubble::Init(DWORD showTime,int faceFrame)
{
	m_iFaceFrame=faceFrame;
	m_dwShowTime=showTime;
	
}
void CChatBubble::Run()
{
	for(int i=0;i<C_PLAYER_COUNT;i++)
	{
		switch(m_bIsShow[i])
		{
			case eBubbleTypeFace:
			{
				UpdateFaceBubble(i);
			}break;
			case eBubbleTypeText:
			{
				UpdateTextBubble(i);
			}break;
			default:
			{
				continue;
			}
		}
	}
}
void CChatBubble::ShowTextBubble(int chattype,int playerIndex)
{
#ifdef C_USE_CHAT_TEXT
	m_hBubble[playerIndex][0]=UpdateMySprite(m_hBubble[playerIndex][0],
			g_dwChatBubbleSprIndex[playerIndex][eBubbleTypeText-1], 
			&g_ptBubbleTxtBk[playerIndex],
			C_BUBLLE_BK_PRIORITY,
			C_BUBLLE_PALETTE,
			-1);
	m_hBubble[playerIndex][1]=UpdateMySprite(m_hBubble[playerIndex][1],
			g_dwChatTextSprIndex[chattype][playerIndex], 
			&g_ptBubbleText[playerIndex],
			C_BUBLLE_PRIORITY,
			C_BUBLLE_PALETTE,
			-1);
#else
	DelBubble(playerIndex);
	m_hBubble[playerIndex][0]=UpdateMySprite(m_hBubble[playerIndex][0],
		g_chatTxtSpr[chattype].spr[g_dwChatTextSprIndex[chattype].eSprTxtType[m_Scene][playerIndex]],//g_dwChatTextSprIndex[chattype].dwSprTxt[playerIndex], 
		(PLPOINT)&g_ptBubbleTxtBk[m_Scene][playerIndex],
		C_BUBLLE_PRIORITY,
		C_BUBLLE_PALETTE,
		-1);
	if(m_hBubble[playerIndex][1]!=INVALID_HSPRITE_VALUE)
	{
		DeleteMySprite(m_hBubble[playerIndex][1]);
		m_hBubble[playerIndex][1]=INVALID_HSPRITE_VALUE;
	}
#endif 

	m_dwStartTime[playerIndex]=GetTickCount();
	m_bIsShow[playerIndex]=eBubbleTypeText;
}
void CChatBubble::ShowFaceBubble(int chattype,int playerIndex)
{
	DelBubble(playerIndex);
	m_hBubble[playerIndex][0]=UpdateMySprite(m_hBubble[playerIndex][0],
			g_dwChatBubbleSprIndex[playerIndex][eBubbleTypeFace-1], 
			(PLPOINT)&g_ptBubbleFaceBk[m_Scene][playerIndex],
			C_BUBLLE_BK_PRIORITY,
			C_BUBLLE_PALETTE,
			-1);
	m_FaceTab[playerIndex]=g_FaceTab[chattype-C_TEXT_COUNT];
	m_hBubble[playerIndex][1]=UpdateMySprite(m_hBubble[playerIndex][1],
			m_FaceTab[playerIndex].dwSprStart,//GetSprId(&m_FaceTab[playerIndex]), 
			(PLPOINT)&(g_ptBubbleFaceBk[m_Scene][playerIndex]+g_ptBubbleFace),
			C_BUBLLE_PRIORITY,
			C_BUBLLE_PALETTE,
			-1);
	SPRSCALEPARA	scale	;
	scale.dwZoomType = ZOOM_TYPE_ONETIME	;
	scale.nAdjX = 0	;
	scale.nAdjY = 0	;
	scale.lleft = 0	;
	scale.ltop = 0	;
	scale.lright = 1024	;
	scale.lbottom = 576	;
	scale.dwZoomXPara = 170	;
	scale.dwZoomYPara = 170	;
	::SetMySpriteScale(m_hBubble[playerIndex][1], &scale)	;

	m_dwStartTime[playerIndex]=GetTickCount();
	m_bIsShow[playerIndex]=eBubbleTypeFace;
	m_ChatType[playerIndex]=chattype;
}
void CChatBubble::UpdateTextBubble(int playerIndex)
{
	DWORD time=GetTickCount()- m_dwStartTime[playerIndex];
	if(time>m_dwShowTime)
	{
		DelBubble(playerIndex);
	}
}
void CChatBubble::UpdateFaceBubble(int playerIndex)
{
	DWORD time=GetTickCount()- m_dwStartTime[playerIndex];
	if(time>m_dwShowTime)
	{
		if(m_hBubble[playerIndex][0]!=INVALID_HSPRITE_VALUE)
		{
			DeleteMySprite(m_hBubble[playerIndex][0]);
			m_hBubble[playerIndex][0]=INVALID_HSPRITE_VALUE;
		}
		if(m_hBubble[playerIndex][1]!=INVALID_HSPRITE_VALUE)
		{
			DeleteMySprite(m_hBubble[playerIndex][1]);
			m_hBubble[playerIndex][1]=INVALID_HSPRITE_VALUE;
		}
		m_bIsShow[playerIndex]=eBubbleTypeNull;
	}else 
	{
		int index=m_ChatType[playerIndex]-C_TEXT_COUNT;
		int frame=((time*C_FACEFRAME)/1000)%(g_FaceTab[index].dwSprEnd-g_FaceTab[index].dwSprStart+3);
		//printf("frame=%d======\n",frame);
		DWORD sprId=g_FaceTab[index].dwSprStart;
		if(frame>g_FaceTab[index].dwSprEnd-g_FaceTab[index].dwSprStart)
		{
			sprId=g_FaceTab[index].dwSprEnd;
		}
		else if(frame<0)
		{
			sprId=g_FaceTab[index].dwSprStart;
		}else
		{
			sprId=g_FaceTab[index].dwSprStart+frame;
		}
		m_hBubble[playerIndex][1]=UpdateMySprite(m_hBubble[playerIndex][1],
				sprId, 
				(PLPOINT)&(g_ptBubbleFaceBk[m_Scene][playerIndex]+g_ptBubbleFace),
				C_BUBLLE_PRIORITY,
				C_BUBLLE_PALETTE,
				-1);
		SPRSCALEPARA	scale	;
		scale.dwZoomType = ZOOM_TYPE_ONETIME	;
		scale.nAdjX = 0	;
		scale.nAdjY = 0	;
		scale.lleft = 0	;
		scale.ltop = 0	;
		scale.lright = 1024	;
		scale.lbottom = 576	;
		scale.dwZoomXPara = 170	;
		scale.dwZoomYPara = 170	;
		::SetMySpriteScale(m_hBubble[playerIndex][1], &scale)	;
	}
}

void CChatBubble::ShowBubble(int chattype,int playerIndex,int sex)
{
	int effect;
	if (chattype>=C_TEXT_COUNT)
	{
		ShowFaceBubble(chattype,playerIndex);
		if (sex==eSexboy)
		{
			effect=g_FaceTab[chattype-C_TEXT_COUNT].dwEffect_boy;
		} 
		else if(sex==eSexGirl)
		{
			effect=g_FaceTab[chattype-C_TEXT_COUNT].dwEffect_girl;
		}else
		{
			effect=m_iSex[playerIndex];
		}
		
		if (effect>=0)
		{
			g_Sound.SoundPlay(Sound_Effect,effect,1);
		}
	} 
	else if(chattype>=0)
	{
		ShowTextBubble(chattype,playerIndex);
		if (sex==eSexboy)
		{
			effect=g_dwChatTextSprIndex[chattype].dwEffect_boy;
		} 
		else if(sex==eSexGirl)
		{
			effect=g_dwChatTextSprIndex[chattype].dwEffect_girl;
		}else
		{
			effect=m_iSex[playerIndex];
		}

		if (effect>=0)
		{
			g_Sound.SoundPlay(Sound_Effect,effect,1);
		}
	}
}
void CChatBubble::DelBubble(int playerIndex)
{
	if(m_hBubble[playerIndex][0]!=INVALID_HSPRITE_VALUE)
	{
		DeleteMySprite(m_hBubble[playerIndex][0]);
		m_hBubble[playerIndex][0]=INVALID_HSPRITE_VALUE;
	}
	if(m_hBubble[playerIndex][1]!=INVALID_HSPRITE_VALUE)
	{
		DeleteMySprite(m_hBubble[playerIndex][1]);
		m_hBubble[playerIndex][1]=INVALID_HSPRITE_VALUE;
	}
	m_bIsShow[playerIndex]=eBubbleTypeNull;
}
void CChatBubble::DelAllBubble()
{
	for(int i=0;i<C_PLAYER_COUNT;i++)
	{
		DelBubble(i);
	}
}
void CChatBubble::SetSex(int sex[])
{
	for (int i=0;i<C_PLAYER_COUNT;i++)
	{
		m_iSex[i]=sex[i];
	}
}
void CChatBubble::SetSex(int player,int sex)
{
	m_iSex[player]=sex;
}
void CChatBubble::SetScene(int scene)
{
	m_Scene=scene;
	DelAllBubble();
}
void CChatBubble::Release()
{
	DelAllBubble();
}
//=====================================================

//===========================================================
CChatBubble g_CChatBubble;


